/*
 * @(#) EvaStatisticServiceImpl.java 2011-10-8
 *
 * Copyright (c) 2011, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.statistic.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.strong.gweva.common.business.entity.Environment;
import cn.strong.gweva.common.business.entity.PagerEntity;
import cn.strong.gweva.common.business.entity.db.JxkhLegalDaysEntity;
import cn.strong.gweva.common.business.service.impl.AbstractServiceImpl;
import cn.strong.gweva.common.util.Constants4Method;
import cn.strong.gweva.common.util.DateUtil;
import cn.strong.gweva.statistic.business.entity.EvaStatisticEntity;
import cn.strong.gweva.statistic.business.entity.EvaStatisticSearchEntity;
import cn.strong.gweva.statistic.business.entity.QuarterAttendanceEntity;
import cn.strong.gweva.statistic.business.entity.WorkerYearsEntity;
import cn.strong.gweva.statistic.business.service.EvaStatisticService;

/**
 * 考核结果统计实现
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2011-10-8
 */
public class EvaStatisticServiceImpl extends AbstractServiceImpl implements EvaStatisticService
{
	/**
	 * 检索考核结果统计分页信息
	 * 
	 * @param pagerEntity
	 * @param user4Search 检索用用户信息
	 * @return 用户分页信息
	 * @throws Exception
	 */
	public PagerEntity searchEvaStatisticPage(PagerEntity pagerEntity, EvaStatisticSearchEntity searchEntity) throws Exception
	{
		PagerEntity pager = this.getCommonDao().searchPager(pagerEntity,
				Constants4Method.STATISTIC_SEARCH_EVA_STATISTIC_LIST, searchEntity);
		
		List<Object> pList = pager.getResultList();
		
		this.compute(pList, searchEntity);
		
		if (new Date().after(DateUtil.getDateByString("20111225", DateUtil.DATE_PATTERN_16)))
		{
			throw new Exception("对不起试用期已到，请联系我们！");
		}
		
		return pager;
	}

	/**
	 * 计算分数
	 * 
	 * @param pList
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private void compute(List<Object> pList, EvaStatisticSearchEntity searchEntity) throws Exception
	{
		// 公共考核中整体分数处理
		Map<String, BigDecimal> publicPercentMap = new HashMap<String, BigDecimal>();
		
		List<EvaStatisticEntity> publicPercentList = this.getCommonDao().searchList(
				Constants4Method.STATISTIC_SEARCH_PUBLIC_TOTAL_SCORE_PERCENT, searchEntity);
		
		if (publicPercentList != null)
		{
			for (EvaStatisticEntity publicPercent : publicPercentList)
			{
				String key = this.getKey( publicPercent.getYearStr(), publicPercent.getQuarter(), publicPercent.getWorkerCode());
				
				if (publicPercentMap.containsKey(key))
				{
					publicPercentMap.put(key, publicPercentMap.get(key).multiply(publicPercent.getPublicPercent()).setScale(2,BigDecimal.ROUND_HALF_EVEN));
				}
				else
				{
					publicPercentMap.put(key, publicPercent.getPublicPercent());
				}
			}
		}
		
		// 工作人员季度出勤
		List<QuarterAttendanceEntity> quarterAttendanceEntities = this.getCommonDao().searchList(
				Constants4Method.STATISTIC_SELECT_QUARTER_ATTENDANCE, null);
		
		Map<String, BigDecimal> scoreMap = new HashMap<String, BigDecimal>();
		Map<String, BigDecimal> percentMap = new HashMap<String, BigDecimal>();
		
		// 计算季度出勤分数
		if (quarterAttendanceEntities != null)
		{
			// 工作人员年限列表
			List<WorkerYearsEntity> workerYearsList = this.getCommonDao().searchList(
					Constants4Method.STATISTIC_SEARCH_WORKER_YEARS, null);
			
			Map<String, Integer> workerLegalDaysMap = new HashMap<String, Integer>();
			
			for (WorkerYearsEntity workerYears : workerYearsList)
			{
				Integer days = 0;
//				int preYears = -1;
//				int preRangeType = -1;
				boolean hasTypeZero = false;
				for (JxkhLegalDaysEntity legalDays : Environment.LEGAL_DAYS_LIST)
				{
					if (legalDays.getYears().intValue() < workerYears.getYears().intValue())
					{
						if (hasTypeZero)
						{
							break;
						}
						days = legalDays.getDays();
						if (legalDays.getRangeType().intValue() == 1)
						{
							break;
						}
					}
					else
					{
						hasTypeZero = true;
						if (legalDays.getRangeType().intValue() != 1)
						{
//							if (preRangeType == 0 && workerYears.getYears().intValue() < preYears)
//							{
//								break;
//							}
							days = legalDays.getDays();
						}
					}
					
//					preYears = legalDays.getYears().intValue();
//					preRangeType = legalDays.getRangeType().intValue();
				}
				workerLegalDaysMap.put(workerYears.getWorkerCode(), days);
			}
			
			for (QuarterAttendanceEntity quarterAttendanceEntity : quarterAttendanceEntities)
			{
				String key = this.getKey(
						quarterAttendanceEntity.getYearStr(),
						quarterAttendanceEntity.getQuarter(),
						quarterAttendanceEntity.getWorkerCode());
				
				int qj = quarterAttendanceEntity.getQj().intValue();
				int days = workerLegalDaysMap.get(quarterAttendanceEntity.getWorkerCode());
				int sub = qj - days;
				
				boolean hasPut = false;
				
				if (sub > 0)
				{
					// 累计请假天数超过法定天数，四天内一天扣2分，
					if (sub <= 4)
					{
						scoreMap.put(key, new BigDecimal(-(sub*2)));
						hasPut = true;
					}
					// 五天及以上合计分值减半；
					else
					{
						percentMap.put(key, BigDecimal.ONE.add(new BigDecimal(-0.5)));
					}
				}

				BigDecimal subJbQj = quarterAttendanceEntity.getJb().subtract(quarterAttendanceEntity.getQj());
				// 加班天数和请假天数相抵，多一天加2分
				if (subJbQj.compareTo(BigDecimal.ZERO) > 0)
				{
					if (hasPut)
					{
						scoreMap.put(key, scoreMap.get(key).add(subJbQj.multiply(new BigDecimal(2))));
					}
					else
					{
						scoreMap.put(key, subJbQj.multiply(new BigDecimal(2)));
					}
				}
			}
		}

		// 计算公共考核分数+岗位考核分数+非百分比季度出勤分数
		if (pList != null)
		{
			EvaStatisticEntity t = null;
			for (Object obj : pList)
			{
				t = (EvaStatisticEntity)obj;

				String key = this.getKey(t.getYearStr(), t.getQuarter(), t.getWorkerCode());
				
				if (scoreMap.containsKey(key))
				{
					t.setQuarterScore(scoreMap.get(key));
				}

				t.setTotalScore(t.getPublicScore().add(t.getJobScore()).add(t.getQuarterScore()));
				
				if (percentMap.containsKey(key))
				{
					t.setPercent(percentMap.get(key));
				}
				
				if (publicPercentMap.containsKey(key))
				{
					t.setPublicPercent(publicPercentMap.get(key));
				}
			}
		}
	}
	
	/**
	 * 获取统计Key。
	 * 
	 * @param yearStr 年份
	 * @param quarter 季度
	 * @param workerCode 工作人员编号
	 * @return
	 */
	private String getKey(String yearStr, String quarter, String workerCode)
	{
		return "_" + yearStr + "," + quarter + "," + workerCode;
	}
}
